package vsa.migrations.junit;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.emf.ecore.resource.Resource;
import org.emftext.language.java.containers.CompilationUnit;

public class JunitMigrationResult {

	private HashMap<Resource, Collection<String>> listsPerResource = new HashMap<Resource, Collection<String>>();

	private static Logger LOGGER = Logger.getLogger(JunitMigrationResult.class.getName());

	private List<CompilationUnit> migratedUnits = new ArrayList<CompilationUnit>();
	
	long beforeTime = -1;
	
	long duration = -1;

	
	public JunitMigrationResult () {
		beforeTime = System.currentTimeMillis();
	}
	
	public void finish () {
		duration = System.currentTimeMillis() - beforeTime;
	}
	
	public long getDuration () {
		return duration;
	}

	public void addError (final String message, Resource resource, Throwable throwable) {

		if (throwable != null)
			LOGGER.log(Level.SEVERE, throwable.getLocalizedMessage(), throwable);

		Collection<String> errorcollection = listsPerResource.get(resource);
		if (errorcollection == null)
			errorcollection = new ArrayList<String>();
		errorcollection.add(message);

		listsPerResource.put(resource, errorcollection);
	}

	public void addMigratedUnit (final CompilationUnit compilationunit) {
		migratedUnits.add(compilationunit);
	}

	public Collection<CompilationUnit> getMigratedCompilationUnits () {
		return migratedUnits;
	}

	public boolean isOk () {
		return listsPerResource.isEmpty();
	}

	public String logMessage () {
		String error = "Following errors occurred:\n";

		for (Resource nextResource : listsPerResource.keySet()) {
			error += nextResource.getURI().toFileString() + ":\n";


			for (String nextError: listsPerResource.get(nextResource))
			  error += "  - " + nextError + "\n";

			error += "\n";
		}

		return error;
	}



}
